package com.wages.project.system.attence.controller;

import java.util.Date;
import java.util.List;
import java.util.Objects;

import com.wages.common.constant.ShiroConstants;
import com.wages.common.utils.DateUtils;
import com.wages.project.monitor.online.domain.OnlineSession;
import com.wages.project.system.user.domain.UserRole;
import com.wages.project.system.user.mapper.UserRoleMapper;
import com.wages.project.system.user.service.IUserService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.wages.framework.aspectj.lang.annotation.Log;
import com.wages.framework.aspectj.lang.enums.BusinessType;
import com.wages.project.system.attence.domain.Attence;
import com.wages.project.system.attence.service.IAttenceService;
import com.wages.framework.web.controller.BaseController;
import com.wages.framework.web.domain.AjaxResult;
import com.wages.common.utils.poi.ExcelUtil;
import com.wages.framework.web.page.TableDataInfo;

import javax.servlet.http.HttpServletRequest;

/**
 * attenceController
 *
 * @author wages
 * @date 2021-05-20
 */
@Controller
@RequestMapping("/system/attence")
public class AttenceController extends BaseController
{
    private String prefix = "system/attence";

    @Autowired
    private IAttenceService attenceService;
    @Autowired
    private IUserService userService;

//    @RequiresPermissions("system:attence:view")
    @GetMapping()
    public String attence()
    {
        return prefix + "/attence";
    }

    /**
     * 查询attence列表
     */
    @RequiresPermissions("system:attence:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Attence attence, HttpServletRequest request)
    {
        OnlineSession onlineSession = (OnlineSession)request.getAttribute(ShiroConstants.ONLINE_SESSION);
        List<UserRole> userRoles = userService.selectUserRoleByUserId(onlineSession.getUserId());
        attence.setUserId(onlineSession.getUserId());
        if (CollectionUtils.isNotEmpty(userRoles)) {
            long count = userRoles.stream().filter(userRole -> Objects.equals(userRole.getRoleId(), 1L)).count();
            if (count > 0) {
                attence.setUserId(null);
            }
        }
        startPage();
        List<Attence> list = attenceService.selectAttenceList(attence);
        return getDataTable(list);
    }

    /**
     * 导出attence列表
     */
//    @RequiresPermissions("system:attence:export")
    @Log(title = "attence", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Attence attence)
    {
        List<Attence> list = attenceService.selectAttenceList(attence);
        ExcelUtil<Attence> util = new ExcelUtil<Attence>(Attence.class);
        return util.exportExcel(list, "attence");
    }

    /**
     * 新增attence
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存attence
     */
//    @RequiresPermissions("system:attence:add")
    @Log(title = "attence", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Attence attence)
    {

        return toAjax(attenceService.insertAttence(attence));
    }

    /**
     * 修改attence
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        Attence attence = attenceService.selectAttenceById(id);
        mmap.put("attence", attence);
        return prefix + "/edit";
    }

    /**
     * 修改保存attence
     */
//    @RequiresPermissions("system:attence:edit")
    @Log(title = "attence", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Attence attence)
    {
        return toAjax(attenceService.updateAttence(attence));
    }

    /**
     * 删除attence
     */
//    @RequiresPermissions("system:attence:remove")
    @Log(title = "attence", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(attenceService.deleteAttenceByIds(ids));
    }


    /**
     * 上班打卡
     */
//    @RequiresPermissions("system:attence:clickIn")
    @Log(title = "attence", businessType = BusinessType.INSERT)
    @PostMapping("/clickIn")
    @ResponseBody
    public AjaxResult clickIn( HttpServletRequest request)
    {
        OnlineSession onlineSession = (OnlineSession)request.getAttribute(ShiroConstants.ONLINE_SESSION);
        Attence attence = new Attence();
        attence.setClockIn(1);// 上班打卡
        attence.setClockInTime(new Date());
        attence.setUserId(onlineSession.getUserId());
        attence.setLoginName(onlineSession.getLoginName());
        attence.setCreateTime(new Date());
        attence.setUpdateTime(new Date());
        attenceService.insertAttence(attence);

        return AjaxResult.success();
    }

    /**
     * 下班打卡
     */
//    @RequiresPermissions("system:attence:clickOut")
    @Log(title = "attence", businessType = BusinessType.INSERT)
    @PostMapping("/clickOut")
    @ResponseBody
    public AjaxResult clickOut( HttpServletRequest request)
    {
        OnlineSession onlineSession = (OnlineSession)request.getAttribute(ShiroConstants.ONLINE_SESSION);
        Attence attence = attenceService.selectAttenceStatus(onlineSession.getUserId(), DateUtils.getStartDate(new Date()), DateUtils.getEndDate(new Date()));
        if (Objects.nonNull(attence)) {
            attence.setClockOut(1);
            attence.setClockOutTime(new Date());
            attence.setUpdateTime(new Date());

            attenceService.updateAttence(attence);
        }

        return toAjax(attenceService.insertAttence(attence));
    }

    /**
     * 查询打卡状态
     */
//    @RequiresPermissions("system:attence:clickStatus")
    @Log(title = "attence", businessType = BusinessType.INSERT)
    @PostMapping("/clickStatus")
    @ResponseBody
    public AjaxResult clickStatus( HttpServletRequest request)
    {
        // 未打卡：0 上班已打卡：1 下班已打卡：2
        int isClick = 0;
        OnlineSession onlineSession = (OnlineSession)request.getAttribute(ShiroConstants.ONLINE_SESSION);
        Attence attence = attenceService.selectAttenceStatus(onlineSession.getUserId(), DateUtils.getStartDate(new Date()), DateUtils.getEndDate(new Date()));
        if (Objects.nonNull(attence)) {
            if (Objects.equals(1,attence.getClockIn())) {
                isClick  = 1;
            }
            if (Objects.equals(1, attence.getClockOut())) {
                isClick = 2;
            }

        }

        return AjaxResult.success(isClick);
    }
}
